package com.ruoyi.message.general.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.message.api.produce.IMessageApiService;
import com.ruoyi.message.api.produce.enums.MessageSendRecordStatusEnum;
import com.ruoyi.message.api.produce.enums.MessageTemplateTypeEnum;
import com.ruoyi.message.general.entity.MessageSendRecord;
import com.ruoyi.message.general.properties.MessageProperties;
import com.ruoyi.message.manage.service.ManageMessageSendRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class MessageSendService {

    @Autowired
    private ManageMessageSendRecordService manageMessageSendRecordService;
    @Autowired
    private MailSender mailSender;
    @Autowired
    private MessageProperties messageProperties;

    @Autowired
    private DxbService dxbService;

    @Autowired
    private JtdService jtdService;

    @Value(value = "${spring.mail.username}")
    private String mailUsername;


    public void sendMessage(String messageId) {
        LambdaQueryWrapper<MessageSendRecord> messageSendRecordLambdaQueryWrapper = Wrappers.lambdaQuery(MessageSendRecord.class)
                .eq(MessageSendRecord::getMessageId, messageId);
        MessageSendRecord messageSendRecord = manageMessageSendRecordService.getByObj(messageSendRecordLambdaQueryWrapper);
        if (messageSendRecord == null) {
            return;
        }
        if (!messageSendRecord.getStatus().equals(MessageSendRecordStatusEnum.SEND.getCode())) {
            return;
        }
        boolean isSendOk = false;
        if (messageProperties.getDev()) {
            if (messageSendRecord.getType().equals(MessageTemplateTypeEnum.SMS_TYPE.getCode()) || messageSendRecord.getType().equals(MessageTemplateTypeEnum.EMAIL_TYPE.getCode())) {
                isSendOk = true;
            } else {
                isSendOk = false;
                log.error("消息类型不存在");
            }
        } else {
            if (messageSendRecord.getType().equals(MessageTemplateTypeEnum.SMS_TYPE.getCode())) {
                if (messageSendRecord.getChannel().equals("短信宝")) {
                    if (messageSendRecord.getAreaCode().equals(IMessageApiService.DEFAULT_AREA_CODE)) {
                        isSendOk = dxbService.sendSms(messageSendRecord.getReceiver(), messageSendRecord.getContent());
                    } else {
                        isSendOk = dxbService.sendWSms(messageSendRecord.getAreaCode() + messageSendRecord.getReceiver(), messageSendRecord.getContent());
                    }
                } else if (messageSendRecord.getChannel().equals("聚通达")) {
                    if (messageSendRecord.getAreaCode().equals(IMessageApiService.DEFAULT_AREA_CODE)) {
                        isSendOk = jtdService.sendSms(messageSendRecord.getReceiver(), messageSendRecord.getContent());
                    } else {
                        isSendOk = true;
                        log.info("聚通达暂不支持国际短信");
                    }
                } else {
                    isSendOk = false;
                    log.error("渠道不存在");
                }
            } else if (messageSendRecord.getType().equals(MessageTemplateTypeEnum.EMAIL_TYPE.getCode())) {
                SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
                simpleMailMessage.setFrom(mailUsername);
                simpleMailMessage.setTo(messageSendRecord.getReceiver());
                simpleMailMessage.setSubject(messageSendRecord.getCode());
                simpleMailMessage.setText(messageSendRecord.getContent());
                mailSender.send(simpleMailMessage);
                isSendOk = true;
            } else {
                isSendOk = false;
                log.error("消息类型不存在");
            }
        }
        LambdaUpdateWrapper<MessageSendRecord> messageSendRecordLambdaUpdateWrapper = Wrappers.lambdaUpdate(MessageSendRecord.class)
                .eq(MessageSendRecord::getMessageId, messageId)
                .set(MessageSendRecord::getStatus, isSendOk ? MessageSendRecordStatusEnum.SEND_OK.getCode() : MessageSendRecordStatusEnum.SEND_FAIL.getCode());
        manageMessageSendRecordService.update(null, messageSendRecordLambdaUpdateWrapper);
    }
}
